package com.ayuku.retail.ui.stock;

import java.util.List;

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.TableItem;

import com.ayuku.commons.AYConst;
import com.ayuku.commons.ListFormMaster;
import com.ayuku.commons.Util;
import com.ayuku.retail.business.stock.StockImpl;
import com.ayuku.retail.persistence.model.Stock;


public class ListStock extends ListFormMaster {
	private FormStock frm;

	public ListStock(Shell parent, int style) {
		super(parent, style);
	}

	@Override
	public void showEditForm() {
		TableItem[] selection = tableData.getSelection();

		if ((selection != null) && (selection.length > 0)) {
			TableItem itm = (TableItem) selection[0];
			frm = new FormStock(getDialogShell(), SWT.NULL,
					AYConst.MODE_VIEW);
			frm.setFormKey(itm.getText(0));
			frm.setFuncId(getFuncId());
			frm.open();
		} else {
			Util.showMsgBox(AYConst.FAIL_VIEW, AYConst.MSG_WARN,
					getDialogShell());
		}
	}

	@Override
	public Integer doDelete(List<String> lst) throws Exception {
		return StockImpl.delete(lst);
	}

	@Override
	public void showAddForm() {
		frm = new FormStock(getDialogShell(), SWT.NULL, AYConst.MODE_ADD);
		frm.setFuncId(getFuncId());
		frm.open();
	}

	@Override
	public void populateTable(String arg) throws Exception {
		tableData.removeAll();
		List<Stock> lst = StockImpl.getList(arg);
		tableData.setItemCount(lst.size());
		data = new Object[lst.size()][];
		for (int i = 0; i < lst.size(); i++) {
			Stock pj = lst.get(i);
			data[i] = new Object[] { pj.getIdx(), (i + 1), pj.getBarcode(),
					pj.getName(), pj.getCategoryCode(), pj.getPrice(),
					pj.getSellingPrice(), pj.getUnit(), pj.getDiscount() };
		}
	}

	@Override
	public void additionalProcess() {
		getDialogShell().setText("Data Stock");
		getDialogShell().setSize(980, 600);
		tableData.getColumn(1).setAlignment(SWT.RIGHT);
		tableData.getColumn(5).setAlignment(SWT.RIGHT);
		tableData.getColumn(6).setAlignment(SWT.RIGHT);
		tableData.getColumn(8).setAlignment(SWT.RIGHT);
		Util.setCenter(getDialogShell());
	}

	@Override
	public void printPreview() {
	}

	@Override
	public String[] getColumCaption() {
		final String[] titles = { "", "No", "Kode", "Nama", "Kategori",
				"Harga Beli (" + AYConst.CURRENCY_CODE + ")",
				"Harga Jual (" + AYConst.CURRENCY_CODE + ")", "Satuan",
				"Diskon (%)" };
		return titles;
	}

	@Override
	public int[] getColumWidth() {
		int[] i = { 0, 60, 140, 220, 120, 140, 140, 100, 100 };
		return i;
	}

	@Override
	protected void fillTable(Object[] obj, TableItem item) {
		item.setText(0, Util.nullToString(obj[0]));
		item.setText(1, Util.nullToString(obj[1]));
		item.setText(2, Util.nullToString(obj[2]));
		item.setText(3, Util.nullToString(obj[3]));
		item.setText(4, Util.nullToString(obj[4]));
		item.setText(5, Util.formatToNumber(obj[5]));
		item.setText(6, Util.formatToNumber(obj[6]));
		item.setText(7, Util.nullToString(obj[7]));
		item.setText(8, Util.formatToNumber(obj[8]));
	}

}
